<?php
/**
 * App Controller
 * 
 * @author CRIA Groep 1 <project@markvink.nl>
 * @version 1.0
 */
class AppController extends Controller 
{
	/**
    * @var array uses following Components
    */
	var $components = array('RequestHandler', 'Session', 'Auth', 'Email');
	
	/**
    * @var array uses following Helpers
    */
	var $helpers = array('Html', 'Javascript', 'Form', 'Session'); 
	
	/**
    * @var array Paginate settings
    */
	var $paginate = array('limit' => 25);
	
	/**
    * @var array uses following Models
    */
	var $uses = array('User');
	
	/**
	 * Callback function beforeFilter
	 * 
	 * Get triggered after init
	 */
	function beforeFilter() {
		foreach(get_class_methods($this) AS $method) {
			if(strstr($method, 'beforeFilter') && $method != 'beforeFilter') {
				$this->$method();
			}
		}
	}
	
	/**
	 * Callback function beforeFilterMail
	 * 
	 * Get triggered after init
	 * Mail settings
	 */
	function beforeFilterMail() {
		$this->Email->delivery = 'mail';
		$this->Email->smtpOptions = array(        
			'port'=>'25',         
			'timeout'=>'30',        
			'host' => 'mail.laundrywaste.eu',        
			'username'=>'no-reply@laundrywaste.eu',        
			'password'=>'test123',        
		);  
		$this->Email->from = 'Laundrywaste.eu <no-reply@laundrywaste.eu>';
		$this->Email->replyTo = 'support@laundrywaste.eu';
	}
	
	/**
	 * Callback function beforeFilterAuth
	 * 
	 * Get triggered after init
	 * Auth configurations
	 */
	function beforeFilterAuth() {
		if($this->params['controller'] == 'pages') {
			$this->Auth->allow('display');
		}
		if(!empty($this->params['prefix']) && $this->params['prefix'] == 'admin') {
			$this->layout = 'cake';
			if($this->Auth->user('role') != 'admin') {
				$this->Session->setFlash("No access for that location.");            
				$this->redirect(array('controller' => 'pages', 'action' => 'index'));
			}
		}
		$this->Auth->logoutRedirect = '/';
	}
	
	/**
	 * Callback function beforeFilterSecurity
	 * 
	 * Get triggered after init
	 * Security settings
	 */
	function beforeFilterSecurity() {
		$this->Security->blackHoleCallback = 'securityError';
	}
	
	/**
	 * Callback function beforeFilterCheckLaundryAccess
	 * 
	 * Get triggered after init
	 * Access check
	 */
	function beforeFilterCheckLaundryAccess($laundry = null) {
		if(empty($laundry) && empty($this->params['laundry'])) {
			return false;
		}
		if($this->Auth->user()) {
			$laundry = ($laundry) ? $laundry : $this->params['laundry'];
			if(!in_array($this->Auth->user('id'), $this->User->findByLaundryId($laundry))) {
				$this->Session->setFlash("No access for that laundry.");            
				$this->redirect(array('controller' => 'laundries', 'action' => 'index'));
			}
		}
	}
	
	/**
	 * Callback function securityError
	 * 
	 * Get triggered after security error
	 */
	function securityError() {
       $this->cakeError('securityError');
	}
	
	/**
	 * Callback function beforeFilterPages
	 * 
	 * Get triggered after init
	 * Static pages settings
	 */
	function beforeFilterPages() {
		if($this->params['controller'] == 'pages' && $this->params['pass'][0] == 'home') {
			if($this->Auth->user()) {
				$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
			}
		}
		if($this->params['controller'] == 'pages' && $this->params['pass'][0] == 'advice') {
			if(!$this->Auth->user()) {
				$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
			}
		}
	}
}